<?php


namespace app\task\behavior\sharp;

use app\task\model\sharp\Goods as GoodsModel;

/**
 * 商品行为管理
 */
class Good
{
    private $model;
    // 小程序id
    private $wxappId;

    public function run($model)
    {
        if (!$model instanceof GoodsModel) {
            return new GoodsModel and false;
        }
        if (!$model::$wxapp_id) {
            return false;
        }
        $this->model = $model;
        $this->wxappId = $model::$wxapp_id;
        //定时上架
        $this->sale();

        return true;
    }

    /**
     * 定时上架商品
     */
    public function sale()
    {
        $this->model->transaction(function () {
            $this->updateGoodsStatus();
        });
        return true;
    }

    private function updateGoodsStatus()
    {
        $where = [
            'status' => 2,
            'sale_time' => ['<=', time()]
        ];
        $res = $this->model->where($where)->update(['status' => 1]);
        return $res ? true : false;
    }
}